<?php
/**货品列表管理控制器
 * Created by PhpStorm.
 * User: xiangpeng
 * Date: 2016/12/31
 * Time: 20:24
 */

namespace Admin\Controller;


use Common\Controller\AdminController;
use Common\Model\GoodsAttrModel;
use Common\Model\GoodsListsModel;
use Common\Model\TypeAttrModel;

class GoodsListsController extends AdminController
{
    //货品添加方法
    public  function index(){
        //获得当前货品的id
        $prid = I('get.prid');
        //获得当前商品的id
        $gid=I('get.gid');
        $oldData = (new GoodsListsModel())->where('gid='.$gid)->select();
        //调用处理重组数据方法
        $oldData=$this->eachs($oldData);
        //分配数据
        //dd($oldData);die;
        $this->assign('oldData',$oldData);
        if(IS_POST){
            //获得post的数据
            //dd($_POST);DIE;
            $data  = I('post.');
            $grid = '';
            foreach($_POST['temp'] as $k=>$v) {
                $grid .= $v .',';
            }
            $grid = rtrim($grid,',');
            $data['grid']=$grid;
            //dd($data['grid']);DIE;
            $data['gid'] = I('get.gid');
            $data['tid'] = I('get.tid');
            //编辑
            if($prid){
                $data['prid']=$prid;
            }
            //dd($data);die;
            $this->store((new GoodsListsModel()),$data,function(){
                $this->success('添加成功',U('GoodsLists/index',['gid'=>I('get.gid')]));die;
            });
        }
        //将商品属性表和类型属性表关联
        $data = (new TypeAttrModel())->alias('ta')->join('shop_goods_attr ga on ta.taid=ga.taid ')->where("gid={$gid} and type=1")->select();
       //dd($data);DIE;
        //重组数组
        $temp = [];
        foreach($data as $k=>$v){
            $temp[$v['name']][]=$v;
        }
        $key =array_keys($temp);
        //dd($key);DIE;
        $this->assign('key',$key);
        //dd($temp);DIE;
        //编辑获得当前要编辑的该条数据
        //获得当前要编辑的prid
        //$grid = I('get.prid');
        if($prid){
            $oldListData = (new GoodsListsModel())->where("prid={$prid}")->find();
            $oldListData['grid'] = explode(',',$oldListData['grid']);

            //分配数据
            $this->assign('oldListData',$oldListData);
            //dd($oldListData);die;
        }



        //dd($temp);die;
        $this->assign('temp',$temp);
        //加载模板
        $this->display();
    }
    //处理重组数据方法
    public  function eachs($oldData){
        foreach ($oldData as $k=>$v){
            $oldData[$k]['grid'] = explode(',',$v['grid']);
        }
        foreach ($oldData as $k=>$v){
            //dd($v['grid']);DIE;
            foreach ($v['grid'] as $n=>$m){
                //$oldData[$k]['grid'] = 1;
                $oldData[$k]['grid'][$n] = (new GoodsAttrModel())->where("gtid='{$m}'")->getField('gtvalue');
            }

        }
        //dd($oldData);die;
        return $oldData;
    }


    //异步判断是否货品是否已经存在
    public function check(){
        if(IS_AJAX){
            //$where = $where = array('gid' => (int) $_GET['gid'], 'grid' => implode(',', $_GET['spec']));
            $where = ['gid' =>$_GET['gid'],'grid'=>trim(implode(',',$_GET['temp']))];
            //查询数据库
            echo m('goodslist')->where($where)->count() ? 0 : 1;

        }
    }
    //删除方法
    public function del(){
        $prid =I('get.prid');
        if((new GoodsListsModel())->where("prid={$prid}")->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

}